
package cn.edu.imust.loginregisterdemo.mine.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.chrisbanes.photoview.PhotoView;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.base.BaseFragment;
import cn.edu.imust.loginregisterdemo.forgetpwd.PwdMsgActivity;
import cn.edu.imust.loginregisterdemo.login.model.User;
import cn.edu.imust.loginregisterdemo.login.view.LoginActivity;
import cn.edu.imust.loginregisterdemo.personmsg.PersonMsgActivity;
import cn.edu.imust.loginregisterdemo.ui.SettingMsgActivity;
import cn.edu.imust.loginregisterdemo.utils.Utils;

public class MineFragment extends BaseFragment implements View.OnClickListener {


    private RelativeLayout rl_person, rl_pwd, rl_setting, rl_about;
    private Button   log_off;
    private TextView nickname, account;
    private ImageView usernameQrCode;
    private Dialog    dialog;
    private PhotoView ivQrCode;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_mine, container, false);

        initView(contentView);
        initDialog();
        Bmob.initialize(getContext(), "0e22c3864fe77b9e2b6016701158559c");

        initListener();
        User currentUser = BmobUser.getCurrentUser(User.class);
        nickname.setText(currentUser.getUsername());
        account.setText(currentUser.getUsername());
        return contentView;
    }

    private void initView(View contentView) {
        rl_person = contentView.findViewById(R.id.rl_person);
        rl_pwd = contentView.findViewById(R.id.rl_pwd);
        rl_setting = contentView.findViewById(R.id.rl_setting);
        rl_about = contentView.findViewById(R.id.rl_about);
        log_off = contentView.findViewById(R.id.log_off);
        nickname = contentView.findViewById(R.id.t_name);
        account = contentView.findViewById(R.id.t_username);
        usernameQrCode = contentView.findViewById(R.id.username_qrCode);
    }

    private void initListener() {
        rl_person.setOnClickListener(this);
        rl_pwd.setOnClickListener(this);
        rl_setting.setOnClickListener(this);
        rl_about.setOnClickListener(this);
        log_off.setOnClickListener(this);
        usernameQrCode.setOnClickListener(this);
    }

    private void initDialog() {
        dialog = new Dialog(getContext(), R.style.edit_AlertDialog_style);
        dialog.setContentView(R.layout.dialog_layout);
        ivQrCode = dialog.findViewById(R.id.pv_scale);
        ivQrCode.setBackgroundResource(R.mipmap.iv_qr_code);
        //选择true的话点击其他地方可以使dialog消失，为false的话不会消失
        dialog.setCanceledOnTouchOutside(true); // Sets whether this dialog is
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = 40;
        dialog.onWindowAttributesChanged(lp);
        ivQrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_person:
                startActivity(new Intent(getActivity(), PersonMsgActivity.class));
                break;
            case R.id.rl_pwd:
                startActivity(new Intent(getActivity(), PwdMsgActivity.class));
                break;
            case R.id.rl_setting:
                startActivity(new Intent(getActivity(), SettingMsgActivity.class));
                break;
            case R.id.rl_about:
                Utils.showSnackBar(getContext(), v, "关于");
                break;
            case R.id.log_off:
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("提示").setMessage("您确定要退出账号吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                BmobUser.logOut();
                                startActivity(new Intent(getActivity(), LoginActivity.class));
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();

                break;
            case R.id.username_qrCode:
                dialog.show();
                break;
            default:
                break;
        }
    }

}